package com.itheima.webaitliasserver.controller;

import com.itheima.webaitliasserver.pojo.Result;
import com.itheima.webaitliasserver.utils.AliyunOssOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName: xiaoZhang
 * @Description:
 * @Author: Zhang
 * @CreateDate: 2025-07-31 11:27
 */
@Slf4j
@RestController
public class UploaderController {
    @Autowired
    private AliyunOssOperator aliyunOssOperator;
    // 本地存储方案
    //    @PostMapping("/upload")
    //    public Result upload(String name , Integer age , MultipartFile file) throws IOException {
    //        String originName = file.getOriginalFilename();
    //        if(originName == null || originName.trim().equals("")){
    //            return Result.success("上传文件不能为空");
    //        }
    //        String fileId = UUID.randomUUID().toString() + originName.substring(originName.lastIndexOf("."));
    //
    //        String saveFileUrl = "D:/职业生涯学习/后端/javaweb/webAI-Tlias-Server/src/main/resources/static/images/" + fileId;
    //
    //        file.transferTo(new File((saveFileUrl)));
    //        log.info("uploadFile:{},{},{}", name, age, file);
    //        return  Result.success("");
    //    }
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("file = {}", file);
        if(file.isEmpty()){
            return  Result.error("文件不能为空");
        }
        String linkUrl = aliyunOssOperator.upload(file.getBytes() , file.getOriginalFilename());
        Result result = new Result();
        result.setMsg("success");
        result.setData(linkUrl);
        return result;
    }

}